# this script conpiles information about all the data in a frequency directory and makes some plots

import cPickle
import os
from numpy import *
import copy


def str_list_columns(list_of_lists):
    # print a list of lists to a file

    # pad short lists with zeros
    max_length = len(list_of_lists[0])
    for l in list_of_lists:
        if len(l) > max_length:
            max_length = len(l)
    print max_length

    for l in list_of_lists:
        if len(l) < max_length:
            l.extend(list(zeros(max_length - len(l))))

    # print out the columns
    ostring = ' '
    for i in range(max_length):
        ostring += '\n 0\t0\t0\t0\t0\t'
        for l in list_of_lists:
            ostring += str(l[i]) + '\t'
    return ostring
    


# list directories that have -Hz in their names
for directory in os.listdir('../data'):
    if directory.find('-Hz') != -1:
        print directory

freq =  raw_input("Enter the frequency you'd like to compile information on: ")

# get all the cells from thier pickles
cells = []
directories = os.listdir('../data/' + freq + '-Hz')
for directory in  copy.deepcopy(directories):
    if "generation" in directory:
        directories.remove(directory)

for directory in directories:
    cells.append( cPickle.load(open('../data/' + freq + '-Hz/' + directory + '/cell.cPickle')) )
num_cells = len(cells)
ten_percent = int(num_cells/10)
print "There are %d cells in this directory.  %d cells make up the top 10 percent." % (num_cells,ten_percent)

# get the top ten percent of the cells (by score)
ninety = cells[-len(cells)/10:]
eighty = cells[-2*len(cells)/10:-len(cells)/10]
seventy =cells[-3*len(cells)/10:-2*len(cells)/10]
sixty = cells[-4*len(cells)/10:-3*len(cells)/10]
fifty = cells[-5*len(cells)/10:-4*len(cells)/10]

cell_groups = []
cell_groups.append(ninety)
cell_groups.append(eighty)
cell_groups.append(seventy)
cell_groups.append(sixty)
cell_groups.append(fifty)

ofilename = []
ofilename.append("ninety.out")
ofilename.append("eighty.out")
ofilename.append("seventy.out")
ofilename.append("sixty.out")
ofilename.append("fifty.out")

ocount = 0
for top_cells in cell_groups:
    # compile a list of the parameters of the top and bottom cells
    top_parameters_dicts = []
    for i in range(len(str(top_cells[0].chromosome).split(','))):
        top_parameters_dicts.append( {}  )
    for cell in top_cells:
        evp_list =  str(cell.chromosome).split(',')
        for i in range(len(evp_list)):
            evp_item_float = float(evp_list[i])
            if evp_item_float in top_parameters_dicts[i].keys():
                top_parameters_dicts[i][evp_item_float] += 1/float(ten_percent)
            else:
                top_parameters_dicts[i][evp_item_float] = 1/float(ten_percent)

    top_parameter_list_of_lists = []
    for i in range(len(top_parameters_dicts)):
        top_parameter_keys = []
        top_parameter_values = []
        for key in top_parameters_dicts[i].keys():
            top_parameter_keys.append(key)
            top_parameter_values.append(top_parameters_dicts[i][key])
        top_parameter_list_of_lists.append(top_parameter_keys)
        top_parameter_list_of_lists.append(top_parameter_values)

    ostring = str_list_columns(top_parameter_list_of_lists)
    for cell in top_cells:
        ostring += '\n '
        cell.trees[0].calc_avg_synapse_distance()
        # column 1 is score
        # column 2 is average synapse distance
        # column 3 is starting diameter
        # column 4 is longest path
        # column 5 is number of synapses
        ostring += str( cell.score ) + '\t'
        ostring += str( cell.trees[0].avg_synapse_distance ) + '\t'
        ostring += str( cell.trees[0].gene.start_diameter ) + '\t'
        ostring += str( cell.trees[0].longest_path  ) + '\t'
        ostring += str( cell.trees[0].num_synapses  ) + '\t'

    ofile = open(ofilename[ocount], 'w')
    ocount += 1
    ofile.write(ostring)


# output a histogram of general scores
scores = {}
# get scores from the directory names
for directory in directories:
    score = float(directory.split('-')[0])
    if score in scores.keys():
        scores[score] += 1
    else:
        scores[score] = 1

ostring = ' '
for score in scores.keys():
    ostring += str(score) + '\t'
    ostring += str(scores[score]) + '\n'
ofile = open('general_scores_histo.out', 'w')
ofile.write(ostring)
    
